package com.listing.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.listing.domain.PlannedItem;
import com.listing.service.ListingEngine;
import com.listing.taobao.TaobaoProxy;
import com.listing.utils.PagingOption;
import com.listing.utils.PagingResult;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemsInventoryGetResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;

@Results({
	  @Result(location="inventory-items.jsp")
})
public class InventoryItemsAction extends ActionBase{

	public static final int TYPE_ONSALE = 0;
	public static final int TYPE_INVENTORY = 1;
	
	private int type = TYPE_ONSALE;
	private String banner;
	private String sellerCids;
	private String keyword;
	private PagingOption option;
	private PagingResult<PlannedItem> pagingItems;
	
	public String execute() throws Exception
	{
		_log.info("banner: " + banner + ", sellerCids: " + sellerCids + ", keyword: " + keyword);
		_log.info("option: " + option);
		String session = getSessionId();
		long userId = getUserId();
		try {
			if (option == null)
			{
				option = new PagingOption();
			}
			if (TYPE_ONSALE == type)
			{
				ItemsOnsaleGetResponse rsp = TaobaoProxy.getOnSales(session, option.getPageNumber(), option.getLimit(), sellerCids, keyword);
				if (rsp.isSuccess())
				{
					List<Item> items = rsp.getItems();
					_log.info("items: " + String.valueOf(items));
					List<PlannedItem> plannedItems = new ArrayList<PlannedItem>();
					ListingEngine engine = new ListingEngine(userId);
					if (items != null)
					{
						for (Item item : items)
						{
							PlannedItem i = new PlannedItem();
							i.setItem(item);
							i.setPlannedListTime(engine.getFireTime(item.getNumIid()));
							plannedItems.add(i);
						}
					}
					pagingItems = new PagingResult<PlannedItem>();
					pagingItems.setItems(plannedItems);
					pagingItems.setTotal(rsp.getTotalResults());
					pagingItems.setOption(option);
				}
				else
				{
					error(rsp);
				}
			}
			else
			{
				ItemsInventoryGetResponse rsp = TaobaoProxy.getInventory(session, option.getPageNumber(), option.getLimit(), banner, sellerCids, keyword);
				if (rsp.isSuccess())
				{
					List<Item> items = rsp.getItems();
					_log.info("items: " + String.valueOf(items));
					List<PlannedItem> plannedItems = new ArrayList<PlannedItem>();
					ListingEngine engine = new ListingEngine(userId);
					if (items != null)
					{
						for (Item item : items)
						{
							PlannedItem i = new PlannedItem();
							i.setItem(item);
							i.setPlannedListTime(engine.getFireTime(item.getNumIid()));
							plannedItems.add(i);
						}
					}
					pagingItems = new PagingResult<PlannedItem>();
					pagingItems.setItems(plannedItems);
					pagingItems.setTotal(rsp.getTotalResults());
					pagingItems.setOption(option);
				}
				else
				{
					error(rsp);
				}
			}
		} catch (ApiException e) {
			error(e);
		}
		return SUCCESS;
	}

	public void setBanner(String banner) {
		this.banner = banner;
	}

	public String getBanner() {
		return banner;
	}

	public void setSellerCids(String sellerCids) {
		this.sellerCids = sellerCids;
	}

	public String getSellerCids() {
		return sellerCids;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setOption(PagingOption option) {
		this.option = option;
	}

	public PagingOption getOption() {
		return option;
	}

	public void setPagingItems(PagingResult<PlannedItem> pagingItems) {
		this.pagingItems = pagingItems;
	}

	public PagingResult<PlannedItem> getPagingItems() {
		return pagingItems;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}
}
